#include "VideoEncoder.h"


APP_ERROR CallBackVenc(std::shared_ptr<uint8_t>& outDataPtr, uint32_t& outDataSize,  uint32_t& channelId,
                        uint32_t& frameId, void* userData)
{
    LogInfo << "Encoder frame:" << frameId;
    auto image = std::make_shared<Image>(outDataPtr, outDataSize);
    auto instance = (Encoder*)userData;
    instance->SendData(image);
    return APP_ERR_OK;
}

APP_ERROR Encoder::Init() {
    VideoEncodeConfig vEConfig;
    vEConfig.callbackFunc = CallBackVenc;
    vEConfig.width = 1280;
    vEConfig.height = 720;
    // 用户可自定义编码参数
    vEConfig.keyFrameInterval = 50;
    vEConfig.srcRate = 25;
    vEConfig.rcMode = 0;
    vEConfig.maxBitRate = 2000;
    vEConfig.ipProp = 50;
    videoEncoder_ = std::make_shared<VideoEncoder>(vEConfig, 0);
}

APP_ERROR Encoder::Process(const std::shared_ptr<void> &input) {
    auto image = std::static_pointer_cast<Image>(input);
    auto ret = videoEncoder_->Encode(*image, frameId_, this);
    frameId_++;
    return ret;
}

APP_ERROR Encoder::DeInit() {
    videoEncoder_.reset();
    return APP_ERR_OK;
}


std::string Encoder::GetModuleName() {
    return "Encoder";
}